/*
 * jxSwing Project (2012, 2013, 2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.text;

import java.awt.Dimension;
import java.text.ParseException;
import java.util.regex.PatternSyntaxException;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;

import org.jxSwing.i18n.I18N;
import org.jxUtils.JXConstants;

/**
 * @author Denilson Edinaldo Pinto
 * @project jxSwing
 * @package org.jxSwing.component.text
 * @date 31/08/2012 - 19:17:07
 */
public final class CEPField extends FormattedField {
	
	private static final long serialVersionUID = 1963780772391414463L;
	
	/**
     * 
     */
	public CEPField() {
	
		super(getMask());
		
		initGUI();
	}
	
	/**
	 * @param required
	 */
	public CEPField(final boolean required) {
	
		super(getMask(), required);
		
		initGUI();
	}
	
	/**
	 * @return {@link MaskFormatter} to CEP
	 */
	private static MaskFormatter getMask() {
	
		try {
			//
			MaskFormatter maskFormatter = new MaskFormatter("##.###-###");
			maskFormatter.setPlaceholderCharacter('_');
			
			return maskFormatter;
			
		}catch(final ParseException parseException) {
			
			parseException.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setName(I18N.component().cep());
		setSize(new Dimension(80, 25));
		setPreferredSize(new Dimension(80, 25));
		setHorizontalAlignment(CENTER);
		
		//
		setInputVerifier(new InputVerifier() {
			
			@Override
			public boolean verify(final JComponent input) {
			
				//
				if(isEmpty()) {
					
					setValue(null);
					
					return true;
					
				}else {
					
					boolean result = true;
					
					try {
						//
						result = getText().matches(JXConstants.CEP);
						
					}catch(final PatternSyntaxException parseException) {
						
						result = false;
					}
					
					//
					if( !result) {
						
						JOptionPane.showMessageDialog(CEPField.this, I18N.component().invalidValueCEP(getText()));
						
						setValue(null);
						requestFocusInWindow();
					}
					
					return result;
				}
			}
		});
	}
	
	/**
	 * This method never return null
	 * 
	 * @see javax.swing.JFormattedTextField#getValue()
	 */
	@Override
	public String getValue() {
	
		return super.getValue() != null ? super.getValue().toString() : "";
	}
	
	/**
	 * @return {@code getText().replaceAll("\\D", "").trim().isEmpty()}
	 */
	public boolean isEmpty() {
	
		return getText().replaceAll("\\D", "").trim().isEmpty();
	}
}
